/**
 * 
 */
package com.haizhi.user.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.haizhi.bean.TbComment;
import com.haizhi.bean.TbProduct;
import com.haizhi.bean.TbProductData;
import com.haizhi.bean.TbUser;
import com.haizhi.bean.TbUserCollection;
import com.haizhi.bean.TbVr;
import com.haizhi.service.CommentService;
import com.haizhi.service.ProductDataService;
import com.haizhi.service.ProductService;
import com.haizhi.service.UserCollectionService;
import com.haizhi.service.UserService;
import com.haizhi.service.VrService;
import com.haizhi.upload.component.FileUpLoadUtil;
import com.haizhi.upload.component.strategy.PdfStrategy;
import com.haizhi.upload.component.strategy.PictureStrategy;
import com.haizhi.upload.component.strategy.ProductFileStrategy;
import com.haizhi.user.vo.CommentVo;
import com.haizhi.user.vo.ProductEasyVo;
import com.haizhi.user.vo.ProductVo;
import com.haizhi.user.vo.VrVo;
import com.haizhi.user.vo.factory.ProductVoFactory;
import com.haizhi.util.BaseVoUtil;
import com.haizhi.util.MyMapBuilder;
import com.haizhi.util.MyMapBuilder.ErrorCode;
import com.haizhi.util.ResponseEntityUtil;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author jiangjintai
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController {
	
	static Logger logger = Logger.getLogger(ProductService.class);
	@Autowired
	UserService userService;
	@Autowired
	CommentService commentService;
	@Autowired
	VrService vrService;
	@Autowired
	ProductService productService;
	@Autowired
	ProductDataService productDataService;
	@Autowired
	UserCollectionService userControllerService;

	@RequestMapping("/addProduct")
	@ApiOperation(value = "添加作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String, Object>> addProduct(
			@ApiParam(value="作品主题",name="productTitle",required=true)@RequestParam(name="productTitle")String productTitle,
			@ApiParam(value="作品细节",name="productDetails",required=true)@RequestParam(name="productDetails")String productDetails,
			@ApiParam(value="作品状态",name="productStatus",required=true)@RequestParam(name="productStatus")String productStatus, 
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		logger.info("user is " + tbUser.getUserAccount());
		if (productTitle == null || productTitle.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"主题不能为空");
		TbProduct tbProduct = new TbProduct();
		tbProduct.setProduct3dCommentNum(0);
		tbProduct.setProductAddTime(new Date());
		tbProduct.setProductCommentNum(0);
		tbProduct.setProductDetails(productDetails);
		tbProduct.setProductEditTime(new Date());
		tbProduct.setProductForkNum(0);
		tbProduct.setProductIsExcellent(0);
		tbProduct.setProductIsVrModel(0);
		tbProduct.setProductNum(String.valueOf(System.currentTimeMillis()));
		tbProduct.setProductStartNum(0);
		tbProduct.setTbUser(tbUser);
		int productStatusInt = Integer.valueOf(productStatus);
		if (productStatusInt < -1 || productStatusInt > 2)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"作品状态参数异常");
		tbProduct.setProductStatus(productStatusInt);
		tbProduct.setProductTitle(productTitle);	
		productService.save(tbProduct);
		//添加一个作品
		TbProductData tbProductData = new TbProductData();
		tbProductData.setDataAddTime(new Date());
		tbProductData.setDataContent("");
		tbProductData.setDataType(1);
		tbProductData.setDataVersion(0);
		tbProductData.setTbProduct(tbProduct);
		productDataService.save(tbProductData);
		logger.info("添加成功");
		ProductVo productVo = new ProductVoFactory(tbProduct).build();
		return new ResponseEntity<Map<String, Object>>(
				MyMapBuilder.buildSuccessMap("添加成功").put("product", productVo), HttpStatus.OK);
	}

	@RequestMapping("/addProductImg")
	@ApiOperation(value = "添加作品缩略图",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addProductImg(
			@ApiParam(value="作品id",name="producId",required=true)@RequestParam("producId") String productId,
			@ApiParam(value="作品图片",name="productImage",required=true)@RequestParam("productImage") MultipartFile productImage,
			HttpServletRequest request
			){
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "id不存在"),HttpStatus.NOT_FOUND);
		if (!productImage.isEmpty()) {
			FileUpLoadUtil ufuu = new FileUpLoadUtil(new PictureStrategy());
			String flag = ufuu.doUpload(productImage, request
					.getServletContext().getRealPath("/"));
			switch (flag) {
			case "-1":
				return ResponseEntityUtil.buildErrorEntity(
						ErrorCode.paramError, "类型出错");
			case "-2":
				return ResponseEntityUtil.buildErrorEntity(
						ErrorCode.paramError, "尺寸要小于10M");
			case "0":
				return ResponseEntityUtil.buildErrorEntity(
						ErrorCode.paramError, "未知错误");
			default:
				tbProduct.setProductImgUrl(flag);
				break;
			}
			productService.save(tbProduct);
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功"),HttpStatus.OK);
		}else{
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "文件不能为空");
		}
	}
	
	@RequestMapping("/updateProduct")
	@ApiOperation(value = "修改作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String, Object>> updateProduct(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam(name="productId")String productId,
			@ApiParam(value="作品主题",name="productTitle",required=true)@RequestParam(name="productTitle")String productTitle,
			@ApiParam(value="作品细节",name="productDetails",required=true)@RequestParam(name="productDetails")String productDetails,
			@ApiParam(value="作品状态",name="productStatus",required=true)@RequestParam(name="productStatus")String productStatus, 
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		if (productTitle == null || productTitle.trim().isEmpty())
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"主题不能为空");
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		int productStatusInt = Integer.valueOf(productStatus);
		if (productStatusInt < -1 || productStatusInt > 2)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"作品状态参数异常");
		tbProduct.setProductStatus(productStatusInt);
		tbProduct.setProductTitle(productTitle);	
		tbProduct.setProductDetails(productDetails);
		productService.save(tbProduct);
		logger.info("修改成功");
		ProductVo productVo = new ProductVoFactory(tbProduct).build();
		return new ResponseEntity<Map<String, Object>>(
				MyMapBuilder.buildSuccessMap("修改成功").put("product", productVo), HttpStatus.OK);
	}
	
	@RequestMapping("/deleteProduct")
	@ApiOperation(value = "修改作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> deleteProduct(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam(name="productId")String productId
			){
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "查无该作品信息");
		productService.delete(tbProduct);
		logger.info("删除成功");
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("删除成功"),HttpStatus.OK);	
	}
	
	/**
	 * 
	 * jiangjintai
	 * 2016年8月15日
	 * @param productId
	 * @param vrType
	 * @param vrContent
	 * @param vrFile
	 * @param request
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws IntrospectionException
	 */
	@RequestMapping("/addVr")
	@ApiOperation(value = "添加vr数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addVr(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam(name="productId")String productId,
			@ApiParam(value="vr内容",name="vrContent",required=true)@RequestParam(name="vrContent")String vrContent,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbProduct tbProduct = productService.findById(Integer
				.valueOf(productId));
		TbVr tbVr = new TbVr();
		if (tbProduct == null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"该作品id不存在");
		tbProduct.setProductIsVrModel(1);
		tbVr.setTbProduct(tbProduct);
		tbVr.setVrAddTime(new Date());
		tbVr.setVrType(1);
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		sb.append(vrContent);
		sb.append("]");
		tbVr.setVrContent(sb.toString());
		productService.save(tbProduct);
		vrService.save(tbVr);
		VrVo  vrVo = BaseVoUtil.getVo(tbVr,new VrVo());
		vrVo.setProductId(tbProduct.getProductId());
		logger.info("存储成功");
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("存储成功").put("vr", vrVo),HttpStatus.OK);
	}

	
	
	@RequestMapping("/addProductFile")
	@ApiOperation(value = "添加文件数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addProductFile(
			@ApiParam(value="数据文件",name="dataFile",required=false)@RequestParam("dataFile") MultipartFile dataFile,
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="是否是新一版本",name="isNew",required=false)@RequestParam("isNew")String isNew,
			HttpServletRequest request
			){
		TbProduct tbProduct = productService.findById(Integer
				.valueOf(productId));
		TbProductData currentTbProductData = productDataService.findTopByTbProductOrderByDataIdDesc(tbProduct);
		TbProductData tbProductData = new TbProductData();
		if (tbProduct == null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"该作品id不存在");
		// 代表文件
		if (dataFile.isEmpty())
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "文件不能为空");
		FileUpLoadUtil fuu = new FileUpLoadUtil(new ProductFileStrategy());
		String flag = fuu.doUpload(dataFile, request.getServletContext()
				.getRealPath("/"));
		switch (flag) {
		case "-1":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "类型出错");

		case "-2":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "尺寸要小于10M");

		case "0":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "未知错误");


		default:
			tbProductData.setDataContent(flag);
			break;
		}
		int isNewInt = Integer.valueOf(isNew);
		if(isNewInt!=0&&isNewInt!=1)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "isNew参数错误");
	if(isNewInt==1)//代表是新的版本
			tbProductData.setDataVersion(currentTbProductData.getDataVersion()+1);
	else
			tbProductData.setDataVersion(currentTbProductData.getDataVersion());
	tbProductData.setDataType(0);
	tbProductData.setDataAddTime(new Date());
	tbProductData.setTbProduct(tbProduct);
	productDataService.save(tbProductData);
	return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功"),HttpStatus.OK);
	}
	
	
	@RequestMapping("/addProductData")
	@ApiOperation(value = "添加作品数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String, Object>> addProductData(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="数据内容",name="dataContent",required=false)@RequestParam("dataContent")String dataContent,
			@ApiParam(value="是否是新一版本",name="isNew",required=false)@RequestParam("isNew")String isNew,
			HttpServletRequest request) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
		TbProduct tbProduct = productService.findById(Integer
				.valueOf(productId));
		TbProductData tbProductData = new TbProductData();
		if (tbProduct == null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"该作品id不存在");
		TbProductData currentTbProductData = productDataService.findTopByTbProductOrderByDataIdDesc(tbProduct);
		int dataTypeInt =1;
		if (dataTypeInt != 1 && dataTypeInt != 0)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError,
					"数据类型错误");
		int isNewInt = Integer.valueOf(isNew);
		if(isNewInt!=0&&isNewInt!=1)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "isNew参数错误");
		tbProductData.setDataContent(dataContent);

		
		if(isNewInt==1)//代表是新的版本
			tbProductData.setDataVersion(currentTbProductData.getDataVersion()+1);
		else
			tbProductData.setDataVersion(currentTbProductData.getDataVersion());
		tbProductData.setDataType(1);
		tbProductData.setDataAddTime(new Date());
		tbProductData.setTbProduct(tbProduct);
		productDataService.save(tbProductData);
		logger.info("存储成功");
		ProductVo productVo = BaseVoUtil.getVo(tbProduct,new ProductVo());

		
		logger.info("视图封装成功");
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功").put("product", productVo),HttpStatus.OK);	
	}
	
	@RequestMapping("/getJson")
	@ApiOperation(value = "获取json数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getJson(
			@ApiParam(value="数据id",name="dataId",required=true)@RequestParam("dataId")String dataId
			){
		TbProductData tbProductData = productDataService.findById(Integer.valueOf(dataId));
		if(tbProductData==null||tbProductData.getDataType()!=1)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "查无该数据资料");
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("json", tbProductData.getDataContent()),HttpStatus.OK);
	}
	
	@RequestMapping("/startProduct")
	@ApiOperation(value = "收藏作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> startProduct(
			@ApiParam(value="产品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="1表示收藏，0表示取消收藏",name="type",required=true)@RequestParam("type")String type,
			HttpServletRequest request
			){
		Integer typeInt = Integer.valueOf(type);
		if(typeInt!=0&&typeInt!=1)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "type参数异常");
		
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		TbProduct tbProduct= productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "查无该作品id");
		if(typeInt==1){//表示收藏
			//判断是否重复收藏
			TbUserCollection tbUserCollection = null;
			tbUserCollection = userControllerService.findByTbUserAndTbProduct(tbUser,tbProduct);
			if(tbUserCollection!=null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "不可重复收藏");
			tbUserCollection = new TbUserCollection();
			tbUserCollection.setCollectionTime(new Date());
			tbUserCollection.setTbProduct(tbProduct);
			tbUserCollection.setTbUser(tbUser);
			userControllerService.saveAndUpdateTbProduct(tbUserCollection);
			logger.info("创建成功");
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("收藏成功"),HttpStatus.OK);
		}else{
			userControllerService.deleteAndUpdateTbProduct(tbProduct,tbUser);
			logger.info("删除成功");
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("删除成功"),HttpStatus.OK);
		}
	}
	
	@RequestMapping("/forkProduct")
	@ApiOperation(value = "跟随作品",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> forkProduct(
			@ApiParam(value="产品id",name="productId",required=true)@RequestParam("productId")String productId,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUser  = (TbUser) request.getAttribute("tbUser");
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该作品id不存在");
		TbProduct tbProductCopy = new TbProduct();
		tbProductCopy.setProduct3dCommentNum(0);
		tbProductCopy.setProductAddTime(new Date());
		tbProductCopy.setProductCommentNum(0);
		tbProductCopy.setProductDetails(tbProduct.getProductDetails());
		tbProductCopy.setProductEditTime(new Date());
		tbProductCopy.setProductForkNum(0);
		tbProductCopy.setProductImgUrl(tbProduct.getProductImgUrl());
		tbProductCopy.setProductIsExcellent(0);
		tbProductCopy.setProductIsVrModel(tbProduct.getProductIsVrModel());
		tbProductCopy.setProductNum(String.valueOf(System.currentTimeMillis()));
		tbProductCopy.setProductPdfFileUrl("");
		tbProductCopy.setProductStartNum(0);
		tbProductCopy.setProductStatus(0);
		tbProductCopy.setTbProduct(tbProduct);
		tbProductCopy.setTbUser(tbUser);
		tbProductCopy.setProductTitle(tbProduct.getProductTitle());
		tbProduct.setProductForkNum(tbProduct.getProductForkNum()+1);
		productService.save(tbProduct);
		productService.save(tbProductCopy);
		Set<TbProductData> tbProductDataSet =tbProduct.getTbProductDatas();
		//获取全部数据
		for(TbProductData tbProductData:tbProductDataSet){
			TbProductData tbProductDataCopy= new TbProductData();
			tbProductDataCopy.setDataAddTime(tbProductData.getDataAddTime());
			tbProductDataCopy.setDataContent(tbProductData.getDataContent());
			tbProductDataCopy.setDataType(tbProductData.getDataType());
			tbProductDataCopy.setDataVersion(tbProductData.getDataVersion());
			tbProductDataCopy.setTbProduct(tbProductCopy);
			productDataService.save(tbProductDataCopy);
		}
		
		//获取全部vr数据
		for(TbVr tbVr:tbProduct.getTbVrs()){
			TbVr tbVrCopy = new TbVr();
			tbVrCopy.setTbProduct(tbProductCopy);
			tbVrCopy.setVrAddTime(tbVr.getVrAddTime());
			tbVrCopy.setVrContent(tbVr.getVrContent());
			tbVrCopy.setVrType(tbVr.getVrType());
			vrService.save(tbVrCopy);
		}

		ProductVo productVo = new ProductVoFactory(tbProductCopy).build();
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功").put("product", productVo),HttpStatus.OK);
	}
	
	@RequestMapping("/addComment")
	@ApiOperation(value = "添加评论",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addComment(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="1为普通评论，2为3D评论",name="type",required=true)@RequestParam("type")String type,
			@ApiParam(value="评论",name="content",required=true)@RequestParam("content")String content,
			HttpServletRequest request
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUser = (TbUser) request.getAttribute("tbUser");
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "查无该作品id号"),HttpStatus.OK);
		Integer typeInt = Integer.valueOf(type);
		if(typeInt!=1&&typeInt!=2)
			return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, "类型只能为1或2"),HttpStatus.OK);
		TbComment tbComment =  new TbComment();
		tbComment.setCommentContent(content);
		tbComment.setCommentFloors(1);
		tbComment.setCommentTime(new Date());
		tbComment.setCommentType(typeInt);
		tbComment.setTbProduct(tbProduct);
		tbComment.setTbUser(tbUser);
		commentService.save(tbComment);
		logger.info("添加成功");
		
		CommentVo commentVo = BaseVoUtil.getVo(tbComment, new CommentVo());
		commentVo.setUserId(tbComment.getTbUser().getUserId());
		commentVo.setUserName(tbComment.getTbUser().getUserName());
		commentVo.setUserSex(tbComment.getTbUser().getUserSex());
		
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功").put("comment", commentVo),HttpStatus.OK);
	}
	
	@RequestMapping("/addDocument")
	@ApiOperation(value = "添加文档",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> addDocument(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="pdf文件",name="pdfFile",required=true)@RequestParam("pdfFile")MultipartFile pdfFile,
			HttpServletRequest request
			){
			
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该产品id不存在");
			// 代表文件
		if (pdfFile.isEmpty())
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "文件不能为空");
		FileUpLoadUtil fuu = new FileUpLoadUtil(new ProductFileStrategy());
		String flag = fuu.doUpload(pdfFile, request.getServletContext()
				.getRealPath("/"));
		switch (flag) {
		case "-1":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "类型出错");

		case "-2":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "尺寸要小于10M");

		case "0":
			return ResponseEntityUtil.buildErrorEntity(
					ErrorCode.paramError, "未知错误");


		default:
			tbProduct.setProductPdfFileUrl(flag);
			break;
		}
		productService.save(tbProduct);
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("添加成功"),HttpStatus.OK);
	}
	
	@RequestMapping("/getProductDetail")
	@ApiOperation(value = "获取作品细节",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getProductDetails(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbProduct tbProduct = productService.findById(Integer.valueOf(productId));
		if(tbProduct==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "获取不到该作品id");
		ProductVo productVo = new ProductVoFactory(tbProduct).build();
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("product", productVo),HttpStatus.OK);
	}
	
	@RequestMapping("/getProductData")
	@ApiOperation(value = "获取版本数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getProductData(
			@ApiParam(value="数据版本号",name="dataVersion",required=true)@RequestParam("dataVersion")String dataVersion,
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId
			){
		List<TbProductData> tbProductDataList = productDataService.findByDataVersionAndTbProductProductId(Integer.valueOf(dataVersion),Integer.valueOf(productId));
		if(tbProductDataList==null||tbProductDataList.size()==0)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "查无该版本信息");
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(TbProductData tbProductData:tbProductDataList){
			Map<String,Object> map = new HashMap<String,Object>();
			if(tbProductData.getDataType()==1){
				//代表json
				map.put("json", tbProductData.getDataId());
			}else{
				map.put("file", tbProductData.getDataContent());
			}		
			list.add(map);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("dataList", list),HttpStatus.OK);
	}
	
	@RequestMapping("/getProductCommentList")
	@ApiOperation(value = "获取评论列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getProductCommentList(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId,
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		Page<TbComment> commentPage = commentService.findByTbProductProductId(Integer.valueOf(productId),pageAble);
		List<CommentVo> commentVoList = new ArrayList<CommentVo>();
		List<TbComment> commentList=commentPage.getContent();
		for(TbComment tbComment : commentList){
			CommentVo commentVo = BaseVoUtil.getVo(tbComment, new CommentVo());
			commentVo.setUserId(tbComment.getTbUser().getUserId());
			commentVo.setUserName(tbComment.getTbUser().getUserName());
			commentVo.setUserSex(tbComment.getTbUser().getUserSex());
			commentVoList.add(commentVo);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("comment",commentVoList).put("nowPage", commentPage.getNumber()).put("totalPage", commentPage.getTotalPages()),HttpStatus.OK);
	}
	
	@RequestMapping("/getVr")
	@ApiOperation(value = "获取vr数据",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getVr(
			@ApiParam(value="作品id",name="productId",required=true)@RequestParam("productId")String productId
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbVr tbVr = vrService.findTopByTbProductProductIdOrderByVrIdDesc(productId);
		if(tbVr==null)
			return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "该作品id有误");
		VrVo vrVo = BaseVoUtil.getVo(tbVr, new VrVo());
		vrVo.setProductId(tbVr.getTbProduct().getProductId());
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("vr", vrVo),HttpStatus.OK);
	}
	
	@RequestMapping("/getStartProductList")
	@ApiOperation(value = "获取收藏作品列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getStartProductList(
			@ApiParam(value="用户id，不填默认填入登录用户",name="userId",required=false)@RequestParam(name="userId")String userId,
			HttpServletRequest request,
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUser = null;
		if(userId==null||userId.trim().isEmpty()){
			tbUser = (TbUser) request.getAttribute("tbUser");
		}else{
			tbUser = userService.findById(Integer.valueOf(userId));
			if(tbUser==null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "userId不存在");
		}
		Page<TbUserCollection> tbUserCollectionPage = userControllerService.findByTbUser(tbUser,pageAble);
		List<TbUserCollection> tbUserCollectionList = tbUserCollectionPage.getContent();
		List<ProductEasyVo> productVoList = new ArrayList<ProductEasyVo>();
		for(TbUserCollection tbUserCollection : tbUserCollectionList){
			TbProduct tbProduct = tbUserCollection.getTbProduct();
			System.out.println(tbProduct.getProductId());
			ProductEasyVo productVo = BaseVoUtil.getVo(tbProduct, new ProductEasyVo());
			productVo.setUserId(tbProduct.getTbUser().getUserId());
			productVo.setUserName(tbProduct.getTbUser().getUserName());
			productVo.setUserSex(tbProduct.getTbUser().getUserSex());
			productVoList.add(productVo);
		}
		return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildSuccessMap("获取成功").put("product", productVoList).put("nowPage", tbUserCollectionPage.getNumber()).put("totalPage", tbUserCollectionPage.getTotalPages()),HttpStatus.OK);
	}
	
	@RequestMapping("/getProductList")
	@ApiOperation(value = "获取作品列表",httpMethod="POST")//添加注释这里要把方法写上，string类型大写  
	public ResponseEntity<Map<String,Object>> getProductList(
			@ApiParam(value="用户id，不填默认填入登录用户",name="userId",required=false)@RequestParam(name="userId")String userId,
			HttpServletRequest request,
			@ApiParam(value="页码数，开始为0，默认为0",name="page",required=true)@RequestParam(defaultValue="0",name="page")String page,
			@ApiParam(value="每页数量，默认20",name="size",required=true)@RequestParam(defaultValue="20",name="size")String size,
			@PageableDefault Pageable pageAble
			) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		TbUser tbUser = null;
		if(userId==null||userId.trim().isEmpty()){
			tbUser = (TbUser) request.getAttribute("tbUser");
		}else{
			tbUser = userService.findById(Integer.valueOf(userId));
			if(tbUser==null)
				return ResponseEntityUtil.buildErrorEntity(ErrorCode.paramError, "userId不存在");
		}
		Page<TbProduct> tbProductPage = productService.findByTbUser(tbUser,pageAble);
		List<TbProduct> productList = tbProductPage.getContent();
		List<ProductEasyVo> productVoList = new ArrayList<ProductEasyVo>();
		for(TbProduct tbProduct : productList){
			ProductEasyVo productEasyVo = BaseVoUtil.getVo(tbProduct, new ProductEasyVo());
			productEasyVo.setUserId(tbProduct.getTbUser().getUserId());
			productEasyVo.setUserName(tbProduct.getTbUser().getUserName());
			productEasyVo.setUserSex(tbProduct.getTbUser().getUserSex());
			productVoList.add(productEasyVo);
		}
		return new ResponseEntity<>(MyMapBuilder.buildSuccessMap("获取成功").put("product", productVoList).put("totalPage", tbProductPage.getNumber()).put("nowPage", tbProductPage.getTotalPages()),HttpStatus.OK);
	}
	
	/** 
     * 异常页面控制 
     *  
     * @param runtimeException 
     * @return 
     */  
    @ExceptionHandler(RuntimeException.class)  
    @ResponseBody
    public  ResponseEntity<Map<String,Object>> runtimeExceptionHandler(RuntimeException runtimeException) {  
    	runtimeException.printStackTrace();
    	logger.info(runtimeException.getMessage());
    	return new ResponseEntity<Map<String,Object>>(MyMapBuilder.buildErrorMap(ErrorCode.paramError, runtimeException.getMessage()),HttpStatus.BAD_REQUEST);
         
    }  
}
